package com.hymjweb.demo.hymj.cash_set_manage.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.cash_set_manage.bean.BCashSetPojo;
import com.hymjweb.demo.hymj.cash_set_manage.service.BCashSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/cash_set_manage")
public class BCashSetController extends BaseController {

    @Autowired
    private BCashSetService bCashSetService;

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/cash_set_manage/bcashset";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) throws Exception {
        return bCashSetService.getPageList(param);
    }

    @RequestMapping("/queryCashierByCondition")
    @ResponseBody
    public Object queryCashierByCondition(String key) throws Exception {
        List<Map<String,Object>> cashierList = bCashSetService.queryCashierByCondition(key);
        return  cashierList;
    }


    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true) BCashSetPojo pojo) throws Exception {
        if(pojo==null){
            throwException("收银员添加参数错误!");
        }
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("收银员添加成功!");

        int affectRows = bCashSetService.insert(pojo);

        if(affectRows<= SysConstant.INT_ZERO){
            baseResultMap.setOk(false);
            baseResultMap.setMessage("收银员添加失败!");
        }

        return baseResultMap;
    }


    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody(required = true)BCashSetPojo pojo) throws Exception {
        if(pojo==null){
            throwException("收银员更新参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("收银员更新成功!");

        int affectRows = bCashSetService.update(pojo);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("收银员更新失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) String id) throws Exception {
        if(DataTypeUtil.isEmptyStr(id)){
            throwException("收银员删除参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("收银员删除成功!");

        int affectRows = bCashSetService.delete(id);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("收银员删除失败!");
        }

        return baseResultMap;
    }


}
